package helper
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;

	import mx.events.FlexEvent;
	import mx.events.SliderEvent;

	import view.MainView;

	public class MainHelper extends MainView
	{
		[Bindable]
		private var file:File;

		private var sound:Sound;
		private var channel:SoundChannel;
		private var seURL:String;

		private var vol:Number;

		public function MainHelper()
		{
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE,init);


		}

		private function init(event:FlexEvent):void{
			browseButton.addEventListener(MouseEvent.CLICK,selectfile);
			playButton.addEventListener(MouseEvent.CLICK,soundPlay);
			stopButton.addEventListener(MouseEvent.CLICK,soundStop);

			volumeSlide.addEventListener(SliderEvent.CHANGE,volumeControl);

			file = new File();
			file.addEventListener(Event.SELECT,seSelectHandler);
		}


		public function seSelectHandler(event:Event):void{
			seURL = file.nativePath;
			trace(file.nativePath);
			sound = new Sound();
			channel = new SoundChannel();
			sound.addEventListener(Event.COMPLETE,seLoadHandler);
			sound.load(new URLRequest(seURL));
		}

		public function seLoadHandler(event:Event):void{
			soundPathText.text = seURL + " " + sound.id3.songName
		}

		public function soundPlay(event:MouseEvent):void{
//			soundPathText.text = "playButtonClick";
			try{
				channel = sound.play()
			}catch(e:Error){

			}

		}

		public function soundStop(event:MouseEvent):void{
			channel.stop();
		}


		private function selectfile(event:MouseEvent):void{
			file.browse();
		}

		private function volumeControl(event:SliderEvent):void{
			vol = volumeSlide.value;
			trace(vol,volumeSlide.value);
			try{
				channel.soundTransform = new SoundTransform(vol);
			}catch(e:Error){

			}
		}


	}
}